﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebApplication5.Model;

namespace WebApplication5.Controller
{
    public class personCtr
    {


        dmaa0912DannyEntities2 db = new dmaa0912DannyEntities2();

        public Login GetPerson(string username, string password)
        {
            var person = from p in db.Login
                         where p.userName.Equals(username) && p.password.Equals(password) && p.verfied == true
                         select p;

            return person.FirstOrDefault();
        }

        public List<Login> GetAllEmployee()
        {
            try
            {
                var Employees = from h in db.Login
                             select h;

                return Employees.ToList();
            }
            catch (Exception e)
            {

            }
            return null;
        }

        public void deletePerson(int id)
        {
            Login h = (from v in db.Login
                       where v.id == id
                       select v).FirstOrDefault();
            if (h != null)
            {
                db.Login.Remove(h);
            }
            else
            {
                //nothing
            }

            db.SaveChanges();
        }


        public void addPerson(Login s)
        {
            validationOfLogin(s);

            db.Login.Add(s);
            db.SaveChanges();
        }

        public void deletePerson(Login s)
        {
            db.Login.Remove(s);
            db.SaveChanges();
        }


                public void editPerson(Login loginUpdate)
        {
            validationOfLogin(loginUpdate);

            Login login = db.Login.FirstOrDefault(l => l.id == loginUpdate.id);


            login.firstName = loginUpdate.firstName;
            login.companyId = loginUpdate.companyId;
            login.lastName = loginUpdate.lastName;
            login.password = loginUpdate.password;
            login.phoneNumber = loginUpdate.phoneNumber;
            login.userName = loginUpdate.userName;
            login.Project = loginUpdate.Project;
            login.verfied = loginUpdate.verfied;

            db.SaveChanges();
        }


                public Login GetEmployee(int id)
                {
                    return (from r in db.Login
                            where r.id == id
                            select r).FirstOrDefault();
                }


                internal List<Login> GetAllEmployee(int id)
                {
                    try
                    {
                        var Employees = from h in db.Login
                                       where id == h.id
                                       select h;

                        return Employees.ToList();
                    }
                    catch (Exception e)
                    {

                    }
                    return null;
                }

                private void validationOfLogin(Login l)
                {

                    Regex onlyDigits = new Regex("^[0-9]+$");
                    Regex onlyLetters = new Regex(@"^[a-zA-Z]+$");
                    Regex onlyLettersAndNumbers = new Regex(@"[a-zæøå 0-9]+");

                    if (string.IsNullOrEmpty(l.firstName) || (!onlyLetters.IsMatch(l.firstName)))
                        throw new Exception("Fornavn er ikke korrekt kun bogstaver tilladt");

                    if ((string.IsNullOrEmpty(l.lastName)) || (!onlyLetters.IsMatch(l.lastName)))
                        throw new Exception("Navn er ikke korrekt kun bogstaver tilladt");

                    if (!onlyDigits.IsMatch(l.phoneNumber.ToString()))
                        throw new Exception("indtast et korrekt telefon nummer");

                    if (!onlyDigits.IsMatch(l.companyId.ToString()))
                        throw new Exception("indtast et korrekt company nummer");

                    if (string.IsNullOrEmpty(l.userName))
                        throw new Exception("Indtast et brugernavn");

                    if (string.IsNullOrEmpty(l.password))
                        throw new Exception("Indtast et kodeord");
                }
    }
}